﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using Update.Servers.Gitee;
using Update.Servers.Github;
using Update.Servers.LanZou;
using Update.Servers.Standard;

namespace Update
{
    /// <summary>
    /// 更新服务器工厂类
    /// </summary>
    public static class UpdateServerFactory
    {
        /// <summary>
        /// 创建更新服务器实例
        /// </summary>
        /// <param name="serverType">服务器类型</param>
        /// <param name="config">配置文档</param>
        /// <returns>更新服务器实例</returns>
        public static IUpdateServer CreateServer(string serverType, XDocument config)
        {
            if (config == null || config.Root == null)
                throw new ArgumentNullException(nameof(config));
                
            IUpdateServer server;
            
            switch (serverType.ToLower())
            {
                case "gitee":
                    server = new GiteeServer();
                    server.Initialize(config.Root.Element("GiteeServer"));
                    break;
                    
                case "github":
                    server = new GitHubServer();
                    server.Initialize(config.Root.Element("GitHubServer"));
                    break;
                    
                case "lanzou":
                    server = new LanZouServer();
                    server.Initialize(config.Root.Element("LanZouServer"));
                    break;
                    
                case "standard":
                default:
                    server = new StandardServer();
                    server.Initialize(config.Root.Element("StandardServer"));
                    break;
            }
            
            return server;
        }
        
        /// <summary>
        /// 获取应用程序配置
        /// </summary>
        /// <param name="config">配置文档</param>
        /// <returns>应用程序配置元素</returns>
        public static XElement GetApplicationConfig(XDocument config)
        {
            if (config == null || config.Root == null)
                throw new ArgumentNullException(nameof(config));
                
            return config.Root.Element("Application");
        }
    }
} 